package com.my.study;

import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * 信号量间的同步
 */
public class CustomCheckin {


    public static void main(String[] args) {


        Semaphore semaphore = new Semaphore(3);

        for(int i = 1;i < 5;i++){

            new Thread(new SecurityCheckinThread(i, semaphore)).start();
        }
    }

    private static class SecurityCheckinThread implements Runnable{

        private int num;
        private Semaphore semaphore;

        public SecurityCheckinThread(int num, Semaphore semaphore) {
            this.num = num;
            this.semaphore = semaphore;
        }

        @Override
        public void run() {

            try{
                semaphore.acquire();
                System.out.println("乘客Num：" + num + "  正在查验中---");

                if(num%2 == 0){

                    Thread.sleep(1000);
                    System.out.println("乘客Num：" + num + " 身份可疑");
                }

            }catch (InterruptedException e){
                e.printStackTrace();
            }finally {

                semaphore.release();
                System.out.println("乘客Num：" + num + "   出关成功！");
            }

        }
    }
}